package com.bq.gametrade.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bq.gametrade.HttpLink;
import com.bq.gametrade.R;
import com.bq.gametrade.adapter.TransferContactsListAdapter;
import com.bq.gametrade.base.BaseActivity;
import com.bq.gametrade.bean.TransferContacts;
import com.bq.gametrade.bean.TransferContactsDetails;
import com.bq.gametrade.bean.UserInfoDetails;
import com.bq.gametrade.http.VRHttp;
import com.bq.gametrade.http.VRHttpListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class TransferContactsActivity extends BaseActivity implements View.OnClickListener {


    private ImageView back;
    private TextView tvTitle;

    private ListView lvFriend;
    private LinearLayout layPrompt;

    private ArrayList<TransferContactsDetails> mList = new ArrayList<>();
    private TransferContactsListAdapter adapter;
    private UserInfoDetails userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transfercontacts);
        if(application.isLogin()){
            userInfo = application.getUserInfo();
        }else {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }
        initView();
        getTransferContacts();
    }

    private void initView() {

        back = serchViewById(R.id.image_back);
        back.setOnClickListener(this);
        tvTitle = serchViewById(R.id.title_name);
        tvTitle.setText("朋友");
        layPrompt = serchViewById(R.id.lay_prompt);
        lvFriend = serchViewById(R.id.lv_friendList);
        adapter = new TransferContactsListAdapter(TransferContactsActivity.this, mList);
        lvFriend.setAdapter(adapter);
        lvFriend.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(TransferContactsActivity.this,TransferActivity.class);
                intent.putExtra("friendName",mList.get(i).userName);
                intent.putExtra("friendAccount",mList.get(i).userId);
                setResult(RESULT_OK,intent);
                finish();
            }
        });

    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_back:
                finish();
                break;

        }
    }






    private void getTransferContacts() {
        Map<String, String> params = new HashMap<>();
        params.put("token", userInfo.token);
//        params.put("tels", list);
        showSpotsDialog("正在获取信息····");
        VRHttp.getTransferContactsList(TransferContactsActivity.this, HttpLink.TRANSFER_CONTACTS_URL, params, new VRHttpListener() {
            @Override
            public void onSuccess(Object response, boolean isCache) {
                if (response != null) {
                    TransferContacts userFriend = (TransferContacts) response;
                    if (userFriend.status.equals("0")) {
                        mList = userFriend.account_list;
                        if (mList.size() > 0) {
                            layPrompt.setVisibility(View.GONE);
                            adapter.setData(mList);
                            adapter.notifyDataSetChanged();
//                            SharedPreferencesUtils.saveValue(UserFriendActivity.this, SharedPreferencesUtils.user_phonenumber, "0");
                        } else {
                            layPrompt.setVisibility(View.VISIBLE);
                        }
                    }else if (userFriend.status.equals("99")) {
                        toastShow("登录超时");
                        application.setUserInfo(null);
                        startActivity(new Intent(TransferContactsActivity.this, LoginActivity.class));
                        finish();
                    } else if (userFriend.status.equals("98")) {
                        toastShow("您的账号已在其它设备登录，请重新登录修改您的密码");
                        application.setUserInfo(null);
                        startActivity(new Intent(TransferContactsActivity.this, LoginActivity.class));
                        finish();
                    }
                }

            }

            @Override
            public void onError(String error) {
                super.onError(error);
                toastShow("获取联系人失败");
            }

            @Override
            public void onFinish() {
                super.onFinish();
                closeSpotsDialog();
            }
        });
    }
}
